<?php
require_once('./define/define_table.php');
require_once('./class/Responsable.php');

define("ADD_NEWS_FORM", "Formulaire d'ajout d'une news");
define("MODIFY_NEWS_FORM", "Formulaire de modification d'une news");

class News{

    private $id_news;
    private $id_individu;
    private $id_association;
    private $date_news;
    private $titre;
    private $texte;

    public function News(){
    
    }
    
    public function getIdNews(){
        return $this->id_news;
    }
    
    public function getIdIndividu(){
        return $this->id_individu;
    }
    
    public function getIdAssociation(){
        return $this->id_association;
    }
    
    public function getDateNews(){
        return $this->date_news;
    }
    
    public function getTitre(){
        return $this->titre;
    }
    
    public function getTexte(){
        return $this->texte;
    }
    
    public function setIdNews($id){
        $this->id_news = $id;
    }
    
    public function setIdIndividu($id){
        $this->id_individu = $id;
    }
    
    public function setIdAssociation($id){
        $this->id_association = $id;
    }
    
    public function setDateNews($date){
        $this->date_news = $date;
    }
    
    public function setTitre($titre){
        $this->titre = $titre;
    }
    
    public function setTexte($texte){
        $this->texte = $texte;
    }
    
    public function getAllNews(){
        global $db;
        try{
            $sql = "SELECT id_news, id_individu, id_association, date_news, titre, texte FROM ".NEWS_TABLE;
            $sth = $db->prepare($sql);
            $sth->execute();
            PrintQuery::queryPrint($sth);
            return $sth->fetchAll(PDO::FETCH_CLASS, "News");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }
    
    public function getNews($id){
        global $db;
        try{
            $sql = "SELECT id_news, id_individu, id_association, date_news, titre, texte FROM ".NEWS_TABLE."
            		WHERE id_news = :id_news";
            $sth = $db->prepare($sql);
            $sth->bindValue(":id_news", $id, PDO::PARAM_INT);
            $sth->execute();
            PrintQuery::queryPrint($sth);
            return $sth->fetchObject("News");
        }catch(PDOException $e){
            echo $e.getMessage();
        }
    }
    
    public function addOrModify(){
        global $db;
        try{
            if($this->id_news == null){
                $sql = "INSERT INTO ".NEWS_TABLE." VALUES(null, :id_individu, :id_association, CURDATE(), :titre, :texte)";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
                $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
                $sth->bindValue(":titre", $this->titre, PDO::PARAM_STR);
                $sth->bindValue(":texte", $this->texte, PDO::PARAM_STR);
                
            }
            else{
                $sql = "UPDATE ".NEWS_TABLE." SET id_individu = :id_individu, id_association = :id_association, date_news = :date, titre = :titre, texte = :texte
                 WHERE id_news = :id_news";
                $sth = $db->prepare($sql);
                $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
                $sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
                $sth->bindValue(":date", $this->date_news);
                $sth->bindValue(":titre", $this->titre, PDO::PARAM_STR);
                $sth->bindValue(":texte", $this->texte, PDO::PARAM_STR);
                $sth->bindValue(":id_news", $this->id_news, PDO::PARAM_INT);
            }
            PrintQuery::queryPrint($sth);
            return $sth->execute();
        }catch(PDOException $e){
            echo $e.getMessage();
        }
        
    }
    
    public function getAddFormAssociation(){
        $html = ADD_NEWS_FORM .
            				'<form action="?module=news&action=addNews" method="post">' .
            				'	<table>' .
            				'		<tr>
            							<td>'._ASSOCIATION_.'</td>
            							<td>'.Association::generateSelect().'</td>
            						</tr>'.
            				'		<tr>'.
            				'			<td colspan="2"><input type="submit" />' .
            				'		</tr>' .
            				'	</table>' .
            				'</form>';
        return $html;
    }
    
    public function getAddForm($id_association){
        $html = ADD_NEWS_FORM .
        				'<form action="?module=news&action=doAdd" method="post">' .
        				'		<input type="hidden" name="id_association" value="'.$id_association.'" />
        						<table>' .
        				'		<tr>
        							<td>'._AUTHOR_.'</td>
        							<td>'.Responsable::generateSelectForNews($id_association).'</td>
        						</tr>'.
        				'		<tr>' .
        				'			<td>'._TITRE_.'</td>' .
        				'			<td><input type="text" name="titre" /></td>' .
        				'		</tr>' .
        				'		<tr>
        							<td colspan="2">'._TEXTE_.'<br /><textarea name="texte"></textarea></td>
        						</tr>'.
        				'		<tr>' .
        				'			<td colspan="2"><input type="submit" />' .
        				'		</tr>' .
        				'	</table>' .
        				'</form>';
        return $html;
    }
    
    public function getModifyForm(){
        $html = MODIFY_NEWS_FORM .
            				'<form action="?module=news&action=doModify" method="post">' .
            				'	<table>' .
            				'		<tr>
            							<td>'._ID_NEWS_.'</td>
            							<td><input type="text" readonly value="'.$this->id_news.'" name="id_news" /></td>
            						</tr>
            						<tr>
            							<td>'._ASSOCIATION_.'</td>
            							<td><input type="text" readonly value="'.Association::getAssociation($this->id_association)->getNom().'" name="id_association" /></td>
            						</tr>
            						<tr>
            							<td>Auteur</td>
            							<td>'.Responsable::generateSelectForNews($this->id_association, $this->id_individu).'</td>
            						</tr>'.
            				'		<tr>' .
                            '		<tr>' .
                    		'			<td>Date</td>' .
                    		'			<td><input type="text" name="date" value="'.$this->date_news.'" class="datepicker" /></td>' .
                    		'		</tr>' .
            				'			<td>Titre</td>' .
            				'			<td><input type="text" name="titre" value="'.$this->titre.'" /></td>' .
            				'		</tr>' .
            				'		<tr>
            							<td colspan="2">Texte<br /><textarea name="texte">'.$this->texte.'</textarea></td>
            						</tr>'.
            				'		<tr>' .
            				'			<td colspan="2"><input type="submit" />' .
            				'		</tr>' .
            				'	</table>' .
            				'</form>';
        return $html;
    }
    
    public function delete(){
        global $db;
        $sql = "DELETE FROM ".NEWS_TABLE." WHERE id_news = :id_news";
        $sth = $db->prepare($sql);
        $sth->bindValue(":id_news", $this->id_news, PDO::PARAM_INT);
        $sth->execute();
        PrintQuery::queryPrint($sth);
    }
}
?>
